package com.kws.system.service.merchant.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.domain.merchant.*;
import com.kws.system.domain.merchant.info.QueryServerExamineInfo;
import com.kws.system.domain.merchant.info.QueryServerInfo;
import com.kws.system.mapper.merchant.ServeManagementMapper;
import com.kws.system.service.merchant.ServeManagementService;
import com.kws.system.service.merchant.ServerAuditService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <b>ServeManagementServiceImpl</b>
 *
 * @description: ServeManagementServiceImpl <br>
 * @date: 2024/12/5 15:34 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class ServeManagementServiceImpl extends ServiceImpl<ServeManagementMapper, TbServe> implements ServeManagementService {
    private static final Logger logger = LoggerFactory.getLogger(ServeManagementServiceImpl.class);

    @Autowired
    private ServerAuditService serverAuditService;

    @Override
    public Page<QueryServerInfo> queryServerPage(QueryServerRq rq) {
        Page<QueryServerInfo> page = new Page<>(rq.getPageNum(), rq.getPageSize());
        Page<QueryServerInfo> pageInfo = baseMapper.queryServerPage(page,rq);
        return pageInfo;
    }

    @Override
    public QueryServerExamineInfo queryServerAuditById(Long id) {
        return baseMapper.queryServerAuditById(id);
    }

    @Override
    public boolean deleteServerById(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        updateWrapper.set("update_time",new Date());
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }

    @Override
    public boolean serverAuditFirstInstance(MerchantExamineRq rq) {
        TbServerAudit tbServerAudit = new TbServerAudit();
        tbServerAudit.setServeId(rq.getMerchantId());
        tbServerAudit.setExamineUser(SecurityUtils.getStringUserId());
        tbServerAudit.setExamineStatus(rq.getExamineStatus());
        tbServerAudit.setExamineTime(new Date());
        tbServerAudit.setExamineHierarchy("0");
        tbServerAudit.setRemark(rq.getRemark());
        if ("0".equals(rq.getExamineStatus())){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",rq.getMerchantId());
            updateWrapper.set("audit_status","3");
            baseMapper.update(null,updateWrapper);
        }else if ("1".equals(rq.getExamineStatus())){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",rq.getMerchantId());
            updateWrapper.set("audit_status","1");
            baseMapper.update(null,updateWrapper);
        }
        boolean save = serverAuditService.save(tbServerAudit);

        return save;
    }

    @Override
    public boolean serverAuditSecondInstance(MerchantExamineRq rq) {
        TbServerAudit tbServerAudit = new TbServerAudit();
        tbServerAudit.setServeId(rq.getMerchantId());
        tbServerAudit.setExamineUser(SecurityUtils.getStringUserId());
        tbServerAudit.setExamineStatus(rq.getExamineStatus());
        tbServerAudit.setExamineTime(new Date());
        tbServerAudit.setExamineHierarchy("0");
        tbServerAudit.setRemark(rq.getRemark());
        if ("0".equals(rq.getExamineStatus())){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",rq.getMerchantId());
            updateWrapper.set("audit_status","3");
            baseMapper.update(null,updateWrapper);
        }else if ("1".equals(rq.getExamineStatus())){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.eq("id",rq.getMerchantId());
            updateWrapper.set("audit_status","2");
            baseMapper.update(null,updateWrapper);
        }
        boolean save = serverAuditService.save(tbServerAudit);

        return save;
    }
}
